<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Source Report: {{ source }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            font-size: 10pt;
            line-height: 1.4;
            color: #1f2937;
            background: white;
        }

        .container {
            padding: 20px 30px;
            max-width: 100%;
        }

        .header {
            border-bottom: 3px solid #2563eb;
            padding-bottom: 15px;
            margin-bottom: 25px;
        }

        .header h1 {
            font-size: 24pt;
            color: #1f2937;
            margin-bottom: 8px;
        }

        .header .subtitle {
            font-size: 11pt;
            color: #6b7280;
        }

        .header .generated-at {
            font-size: 9pt;
            color: #9ca3af;
            margin-top: 5px;
        }

        .section {
            margin-bottom: 25px;
            page-break-inside: avoid;
        }

        .section-title {
            font-size: 14pt;
            font-weight: 600;
            color: #1f2937;
            border-bottom: 2px solid #e5e7eb;
            padding-bottom: 8px;
            margin-bottom: 12px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 12px;
            margin-bottom: 15px;
        }

        .stat-card {
            background: #f9fafb;
            border: 1px solid #e5e7eb;
            border-radius: 6px;
            padding: 12px;
        }

        .stat-label {
            font-size: 9pt;
            color: #6b7280;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 6px;
        }

        .stat-value {
            font-size: 18pt;
            font-weight: 700;
            color: #2563eb;
        }

        .stat-value.danger {
            color: #dc2626;
        }

        .stat-value.warning {
            color: #f59e0b;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
            font-size: 9pt;
        }

        th {
            background: #f3f4f6;
            padding: 8px;
            text-align: left;
            font-weight: 600;
            border-bottom: 2px solid #d1d5db;
            font-size: 8pt;
            text-transform: uppercase;
            color: #4b5563;
        }

        td {
            padding: 8px;
            border-bottom: 1px solid #e5e7eb;
        }

        tr:last-child td {
            border-bottom: none;
        }

        .risk-high {
            color: #dc2626;
            font-weight: 600;
        }

        .risk-medium {
            color: #f59e0b;
            font-weight: 600;
        }

        .risk-low {
            color: #10b981;
            font-weight: 600;
        }

        .badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 4px;
            font-size: 8pt;
            font-weight: 600;
        }

        .badge-critical {
            background: #fee2e2;
            color: #991b1b;
        }

        .badge-high {
            background: #fef3c7;
            color: #92400e;
        }

        .badge-medium {
            background: #dbeafe;
            color: #1e40af;
        }

        .badge-low {
            background: #d1fae5;
            color: #065f46;
        }

        .badge-informational {
            background: #f3f4f6;
            color: #4b5563;
        }

        .findings-breakdown {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 15px;
            margin-top: 15px;
        }

        .findings-list {
            background: #f9fafb;
            border: 1px solid #e5e7eb;
            border-radius: 6px;
            padding: 12px;
        }

        .findings-list h4 {
            font-size: 10pt;
            margin-bottom: 8px;
            color: #4b5563;
        }

        .findings-list ul {
            list-style: none;
            padding-left: 0;
        }

        .findings-list li {
            padding: 4px 0;
            font-size: 9pt;
            display: flex;
            justify-content: space-between;
        }

        .finding-name {
            color: #1f2937;
        }

        .finding-count {
            font-weight: 600;
            color: #2563eb;
        }

        .footer {
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #e5e7eb;
            font-size: 8pt;
            color: #9ca3af;
            text-align: center;
        }

        @media print {
            body {
                print-color-adjust: exact;
                -webkit-print-color-adjust: exact;
            }

            .section {
                page-break-inside: avoid;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Source Report: {{ source }}</h1>
            <div class="subtitle">Comprehensive Analysis and Risk Assessment</div>
            <div class="generated-at">Generated: {{ generated_at }}</div>
        </div>

        <!-- Summary Statistics -->
        <div class="section">
            <h2 class="section-title">Summary Statistics</h2>
            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-label">Total Files</div>
                    <div class="stat-value">{{ summary.total_files }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">Total Findings</div>
                    <div class="stat-value">{{ summary.total_findings }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">Verified Findings</div>
                    <div class="stat-value danger">{{ summary.verified_true_positives }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">Untriaged</div>
                    <div class="stat-value warning">{{ summary.not_triaged }}</div>
                </div>
            </div>
        </div>

        <!-- Risk Indicators - Credentials -->
        <div class="section">
            <h2 class="section-title">Credential Exposure</h2>
            <div class="stats-grid" style="grid-template-columns: repeat(5, 1fr);">
                <div class="stat-card">
                    <div class="stat-label">NoseyParker Findings</div>
                    <div class="stat-value {% if risk_indicators.credentials.noseyparker_findings > 0 %}danger{% endif %}">{{ risk_indicators.credentials.noseyparker_findings }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">YARA Matches</div>
                    <div class="stat-value {% if risk_indicators.sensitive_data.yara_matches > 0 %}danger{% endif %}">{{ risk_indicators.sensitive_data.yara_matches }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">Chromium Logins Decrypted</div>
                    <div class="stat-value {% if risk_indicators.credentials.chromium_logins_decrypted > 0 %}danger{% endif %}">{{ risk_indicators.credentials.chromium_logins_decrypted }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">Chromium Cookies Decrypted</div>
                    <div class="stat-value {% if risk_indicators.credentials.chromium_cookies_decrypted > 0 %}danger{% endif %}">{{ risk_indicators.credentials.chromium_cookies_decrypted }}</div>
                </div>
                <div class="stat-card">
                    <div class="stat-label">DPAPI Masterkeys Decrypted</div>
                    <div class="stat-value {% if risk_indicators.credentials.dpapi_masterkeys_decrypted > 0 %}danger{% endif %}">{{ risk_indicators.credentials.dpapi_masterkeys_decrypted }}</div>
                </div>
            </div>
        </div>

        <!-- Findings by Category -->
        {% if findings_detail.by_category %}
        <div class="section">
            <h2 class="section-title">Findings by Category</h2>
            <table>
                <thead>
                    <tr>
                        <th>Category</th>
                        <th style="text-align: right;">Count</th>
                        <th style="text-align: right;">Percentage</th>
                    </tr>
                </thead>
                <tbody>
                    {% for category, count in findings_detail.by_category.items() %}
                    <tr>
                        <td>{{ category }}</td>
                        <td style="text-align: right;">{{ count }}</td>
                        <td style="text-align: right;">{{ "%.1f"|format((count / summary.total_findings * 100) if summary.total_findings > 0 else 0) }}%</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}

        <!-- Findings by Severity -->
        {% if findings_detail.by_severity %}
        <div class="section">
            <h2 class="section-title">Findings by Severity</h2>
            <table>
                <thead>
                    <tr>
                        <th>Severity</th>
                        <th style="text-align: right;">Count</th>
                        <th style="text-align: right;">Percentage</th>
                    </tr>
                </thead>
                <tbody>
                    {% for severity, count in findings_detail.by_severity.items() %}
                    <tr>
                        <td>
                            <span class="{% if severity == 'critical' %}risk-high{% elif severity == 'high' %}risk-medium{% else %}risk-low{% endif %}">
                                {{ severity|capitalize }}
                            </span>
                        </td>
                        <td style="text-align: right;">{{ count }}</td>
                        <td style="text-align: right;">{{ "%.1f"|format((count / summary.total_findings * 100) if summary.total_findings > 0 else 0) }}%</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}

        <!-- Top Findings -->
        {% if top_findings %}
        <div class="section">
            <h2 class="section-title">Top 20 Findings</h2>
            <table>
                <thead>
                    <tr>
                        <th>Finding Name</th>
                        <th>Category</th>
                        <th>Severity</th>
                        <th>Triage Status</th>
                        <th>File Path</th>
                    </tr>
                </thead>
                <tbody>
                    {% for finding in top_findings[:20] %}
                    <tr>
                        <td>{{ finding.finding_name }}</td>
                        <td>{{ finding.category }}</td>
                        <td>
                            {% set severity_name = 'critical' if finding.severity >= 9 else 'high' if finding.severity >= 7 else 'medium' if finding.severity >= 4 else 'low' if finding.severity >= 2 else 'informational' %}
                            <span class="badge badge-{{ severity_name }}">{{ severity_name|upper }}</span>
                        </td>
                        <td>
                            {% if finding.triage_state == 'true_positive' %}
                                <span class="risk-high">Verified</span>
                            {% elif finding.triage_state %}
                                <span>{{ finding.triage_state|replace('_', ' ')|title }}</span>
                            {% else %}
                                <span class="risk-medium">Untriaged</span>
                            {% endif %}
                        </td>
                        <td style="font-size: 8pt;">{{ finding.file_path or 'N/A' }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}

        <!-- AI Risk Assessment -->
        {% if ai_synthesis %}
        <div class="section" style="page-break-before: always;">
            <h2 class="section-title">AI-Generated Risk Assessment</h2>
            <div style="margin-bottom: 12px;">
                <span class="badge {% if ai_synthesis.risk_level == 'high' %}badge-critical{% elif ai_synthesis.risk_level == 'medium' %}badge-high{% else %}badge-medium{% endif %}" style="font-size: 10pt; padding: 6px 12px;">
                    Risk Level: {{ ai_synthesis.risk_level|upper }}
                </span>
            </div>
            <div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 6px; padding: 15px; font-size: 9pt; line-height: 1.6;">
                {{ ai_synthesis.report_html|safe }}
            </div>
        </div>
        {% endif %}

        <div class="footer">
            Generated by Nemesis Reporting System • {{ source }} • {{ generated_at }}
        </div>
    </div>
</body>
</html>
